<?php

namespace app\common\service\shop\mercadolibre;

/**
 * Goods
 * @package app\common\service
 * @Description 美客多跨境平台接口服务-订单相关
 * @author xuli
 * @date 2024-10-21 14 => 40
 */
class Order extends BaseApi
{

    /**
     * 添加商品
     * @return array
     */
    public function getOrderList()
    {
        $filter_list = [
            'buyer' => '',  // 买家ID
            'seller.id' => '', // 卖家ID
            'order.status' => '', // 订单状态 paid, cancelled, payment_required, paid
            'site' => '', // 站点 MLM, MLB, MLC
            'limit' => '', // 数量，最大值50
            'offset' => '', // 第几页
            'date_created.from' => '', // 订单创建时间的开始时间  yyyy-MM-ddThh => mm => ss
            'date_created.to' => '', // 订单创建时间的结束时间
            'last_updated.from' => '', // 订单更新时间的开始时间
            'last_updated.to' => '', // 订单更新时间的结束时间
            'date_closed.from' => '', // 订单结束时间的开始时间
            'date_closed.to' => '', // 订单结束时间的结束时间
            'mediations.stage' => '', //
        ];

        $filter_list_arr = [];
        foreach ($filter_list as $field => $value) {
            if(!empty($value)) {
                $filter_list_arr[] = $field.'='.$value;
            }
        }
        $filter_list_arr = implode(',', $filter_list_arr);

        $result = $this->shopApi('search_orders', '1', ['filter' => $filter_list_arr]);

        $return_data = [
            "query" => "",
            "results" => [
                [
                    "id" => 2194210960,
                    "buyer" => [
                        "id" => 441782523
                    ],
                    "config" => [
                        "items" => [
                            [
                                "id" => "MLM733232983"
                            ]
                        ]
                    ],
                    "orders" => [
                        [
                            "id" => 2194210960,
                            "items" => null,
                            "feedback" => [
                                "purchase" => null,
                                "sale" => null
                            ],
                            "payments" => [
                                [
                                    "id" => 5384783409
                                ]
                            ],
                            "mediations" => [
                                [
                                    "id" => 1041550651
                                ]
                            ]
                        ]
                    ],
                    "shipment" => [
                    "id" => 28140042692,
                        "payments" => []
                    ]
                ],
                [
                    "id" => 2194336280,
                    "buyer" => [
                        "id" => 441782523
                    ],
                    "config" => [
                        "items" => [
                            [
                                "id" => "MLM733232983"
                            ]
                        ]
                    ],
                    "orders" => [
                        [
                            "id" => 2194336280,
                            "items" => null,
                            "feedback" => [
                                "purchase" => null,
                                "sale" => null
                            ],
                            "payments" => [
                                [
                                    "id" => 5384748271
                                ]
                            ],
                            "mediations" => [
                                [
                                    "id" => 5007508763
                                ]
                            ]
                        ]
                    ],
                    "shipment" => [
                        "id" => 28139907607,
                        "payments" => []
                    ]
                ]
            ],
            "sort" => [
                "id" => "date_asc",
                "name" => "Date ascending"
            ],
            "available_sorts" => [
                [
                    "id" => "date_desc",
                    "name" => "Date descending"
                ]
            ],
            "filters" => [],
            "paging" => [
                "total" => 2,
                "limit" => 50,
            ]
        ];
        return $return_data;
    }

    public function getOrderInfo($order_id)
    {
        $result = $this->shopApi('search_orders', '1', ['order_id' => '2000003508419013']);

        $return_data = [
            "id" => 2000003508419013,
            "date_created" => "2020-01-31T18:03:35.000-04:00",
            "date_closed" => "2020-01-31T18:03:36.000-04:00",
            "last_updated" => "2020-01-31T18:03:36.000-04:00",
            "manufacturing_ending_date" => null,
            "feedback" => [
                "sale" => null,
                "purchase" => null
            ],
            "mediations" => [],
            "comments" => null,
            "pack_id" => null,
            "pickup_id" => null,
            "order_request" => [
                "return" => null,
                "change" => null
            ],
            "fulfilled" => null,
            "paid_amount" => 15.1,
            "coupon" => [
                "id" => null,
                "amount" => 0
            ],
            "expiration_date" => "2020-05-10T18:03:36.000-04:00",
            "order_items" => [
                [
                    "item" => [
                    "id" => "MLM754639529",
                        "title" => "Elemento De Prueba - Para Pruebas De Carga",
                        "category_id" => "MLM71792",
                        "variation_id" => null,
                        "seller_custom_field" => null,
                        "variation_attributes" => [],
                        "condition" => "new",
                        "seller_sku" => null,
                        "parent_item_id" => "CBT910504819"
                    ],
                    "quantity" => 1,
                    "unit_price" => 15.1,
                    "full_unit_price" => 15.1,
                    "currency_id" => "USD",
                    "manufacturing_days" => null,
                    "sale_fee" => 2.64,
                    "base_exchange_rate" => 19.25
                ]
            ],
            "currency_id" => "USD",
            "payments" => [
                [
                    "id" => 5855860136,
                    "order_id" => 2000003508419013,
                    "payer_id" => 441782523,
                    "collector" => [
                        "id" => 481240836
                    ],
                    "card_id" => 8738685222,
                    "site_id" => "MLM",
                    "reason" => "Elemento De Prueba - Para Pruebas De Carga",
                    "payment_method_id" => "amex",
                    "currency_id" => "USD",
                    "installments" => 1,
                    "issuer_id" => "157",
                    "atm_transfer_reference" => [
                    "company_id" => null,
                        "transaction_id" => "1234567"
                    ],
                    "coupon_id" => null,
                    "activation_uri" => null,
                    "operation_type" => "regular_payment",
                    "payment_type" => "credit_card",
                    "available_actions" => [
                        "refund"
                    ],
                    "status" => "approved",
                    "status_code" => null,
                    "status_detail" => "accredited",
                    "transaction_amount" => 15.1,
                    "taxes_amount" => 0,
                    "shipping_cost" => 0,
                    "coupon_amount" => 0,
                    "overpaid_amount" => 0,
                    "total_paid_amount" => 15.1,
                    "installment_amount" => 15.1,
                    "deferred_period" => null,
                    "date_approved" => "2020-01-31T18:03:36.000-04:00",
                    "authorization_code" => "1234567",
                    "transaction_order_id" => null,
                    "date_created" => "2020-01-31T18:03:36.000-04:00",
                    "date_last_modified" => "2020-01-31T18:03:36.000-04:00"
                ]
            ],
            "shipping" => [
                "id" => 28237306862
            ],
            "status" => "paid",
            "status_detail" => [
                "code" => "",
                "description" => null
            ],
            "buyer" => [
                "id" => 441782523,
                "nickname" => "TESTY0DT2NRL",
                "last_name" => "Test",
                "first_name" => "Test",
            ],
            "seller" => [
                "id" => 481240836,
            ],
            "taxes" => [
                "amount" => 0,
                "currency_id" => "USD"
            ],
            "context" => [
                "channel" => "marketplace",
                "site" => "MLM",
                "flows" => [
                    "cbt"
                ],
                "application" => "purchases-api"
            ]
        ];
        return $return_data;
    }

}
